/**
 * 
 */
package org.mspring.mlog.web.module.ucenter.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.service.security.UserService;
import org.mspring.mlog.support.resolver.QueryParam;
import org.mspring.mlog.support.resolver.SpaceUser;
import org.mspring.mlog.web.module.ucenter.AbstractUCenterWidget;
import org.mspring.mlog.web.query.PostQueryCriterion;
import org.mspring.platform.persistence.support.Page;
import org.mspring.platform.persistence.support.Sort;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Gao Youbo
 * @since 2013-6-8
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/u/{username}/admin")
public class UCenterAdmin_PostWidget extends AbstractUCenterWidget {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;

    @RequestMapping("/post/list")
    public String list(@SpaceUser(pathVariable = "username") User user, @ModelAttribute Page<Post> postPage, @ModelAttribute Post post, @QueryParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (user == null) {
            return "/errors/404";
        }
        if (postPage == null) {
            postPage = new Page<Post>();
        }
        postPage.setSort(new Sort("isTop desc, id desc", ""));
        // 默认查看已发布的文章
        if (post == null) {
            post = new Post();
        }
        if (queryParams.get("status") == null || StringUtils.isBlank(queryParams.get("status").toString())) {
            post.setStatus(Post.Status.PUBLISH);
            queryParams.put("status", Post.Status.PUBLISH);
        }
        queryParams.put("author.id", user.getId());
        postPage = postService.listPage(new PostQueryCriterion(queryParams), postPage);
        model.addAttribute("postPage", postPage);
        return "/u/admin/post/list";
    }

    @RequestMapping("/create")
    public String create(@PathVariable String username, HttpServletRequest request, HttpServletResponse response, Model model) {
        return null;
    }

    @RequestMapping("/create/do")
    public String create_do(@PathVariable String username, HttpServletRequest request, HttpServletResponse response, Model model) {
        return null;
    }
}
